# from urllib.request import Request, urlopen
# from urllib.parse import quote
#
# def get_html(url):
#     headers = {
#         'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) '
#                       'Chrome/126.0.0.0 Safari/537.36 Edg/126.0.0.0'
#     }
#
#     request = Request(url, headers=headers)
#     response = urlopen(request)
#
#     return response.read().decode()
#
#
# def save_html(html, filename):
#     with open(filename, 'w', encoding='utf-8') as f:
#         f.write(html)
#     print(html)
#
#
# def main():
#     tieba = input('请输入要获取的贴吧：')
#     page = int(input('请输入页数:'))
#     for i in range(0, page):
#         url = 'https://tieba.baidu.com/f?kw=' + quote(tieba) + '&pn={}'.format(i*50)
#         # https://tieba.baidu.com/f?kw=python&ie=utf-8&pn=100
#         # https://tieba.baidu.com/f?kw=c&ie=utf-8&pn=50
#         html = get_html(url)
#         filename = '第' + str(i+1) + '页.html'
#         save_html(html, filename)
#
#
# if __name__ == '__main__':
#     main()
